package de.mhu.mandalaui.properties;

import de.mhu.mandalaui.AbstractComposite;
import de.mhu.mandalaui.Response;
import de.mhu.mandalaui.TextComposit;
import de.mhu.mandalaui.attributes.Attribute;
import de.mhu.mandalaui.attributes.BooleanAttribute;
import de.mhu.mandalaui.text.Text;

public class PropertiesTable extends AbstractComposite<Property,PropertiesTable.Options> implements PropertiesForm {

	private Attribute data;
	private BooleanAttribute editMode;

	@Override
	public void render(Response response) {
		
		response.registerAttribute(editMode);
		
		data.startRender(response);
		setBindWith(data);
		response.startTag("table", "table", this );
		
		for (Container<Property, Options> property : this) {
			response.startTag("tr", null, null);
			response.startTag("td", null, null);
			response.containerEnter(property.getOptions().getTitle());
			property.getOptions().getTitle().render(response);
			response.containerLeave();
			response.endTag("td");
			response.startTag("td", null, null);
			response.containerEnter(property.getItem());
			property.getItem().render(response);
			response.containerLeave();
			response.endTag("td");
			response.endTag("tr");
		}
		response.endTag("table");
		
	}

	public static class Options {
		private TextComposit title;
		
		public Options(String title) {
			this.title = new TextComposit(new Text(title));
		}

		public TextComposit getTitle() {
			return title;
		}

		public void setTitle(TextComposit title) {
			this.title = title;
		}
	}

	protected void doInitialize() {
		super.doInitialize();
		data = new Attribute();
		data.setOwner(this);
	}
	
	protected void doDestroy() {
		super.doDestroy();
		data = null;
	}

	public Attribute getDataAttribute() {
		return data;
	}

	@Override
	public boolean isEditable() {
		return editMode != null;
	}

	@Override
	public BooleanAttribute getBindEditMode() {
		return editMode;
	}
	
	public void setBindEditMode(BooleanAttribute editMode) {
		this.editMode = editMode;
	}

}
